﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EasyPiano
{
    public partial class SoundSelectControl : UserControl
    {
        public string musicSoreText
        {
            get
            {
                return tb_music_score.Text;
            }
            set
            {
                tb_music_score.Text = value;
            }
        }
        public int SelectedIndex
        {
            get
            {
                return cb_sound.SelectedIndex;
            }
            set
            {
                cb_sound.SelectedIndex = value;
            }
        }

        public SoundSelectControl()
        {
            InitializeComponent();
        }

        private void bt_play_Click(object sender, EventArgs e)
        {
            if (cb_sound.SelectedIndex != -1)
            {
                if (cb_sound.SelectedIndex != 0) //0表示不发音
                    Util.AsynPlaySound(cb_sound.SelectedItem.ToString());
            }
        }

        private void tb_music_score_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int index = Util.SoundStringToIndex(tb_music_score.Text);
                if (cb_sound.SelectedIndex != index)
                    cb_sound.SelectedIndex = index;
            }
            catch
            {

            }
        }

        public void Init()
        {
            if(GlobalData.soundsPath!=null)
            {
                foreach (var file in GlobalData.soundsPath)
                {
                    cb_sound.Items.Add(file);
                }
            }           
        }

        private void SoundSelectControl_Load(object sender, EventArgs e)
        {
            Init();
            if (cb_sound.Items.Count > 0)
            {
                //cb_sound.SelectedIndex = 0;
                tb_music_score.Text = "0";
            }
        }

        private void cb_sound_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string txt = Util.SoundIndexToString(cb_sound.SelectedIndex);
                if (tb_music_score.Focused == false && tb_music_score.Text != txt)
                {
                    tb_music_score.Text = txt;
                    tb_music_score.Select(this.tb_music_score.TextLength, 0);
                }
            }
            catch
            {

            }
        }
    }
}
